//
//  CollectViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/14.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectTableViewCell.h"
#import "ToolDB.h"
#import "PlayPictureModel.h"
#import "ToolModel.h"
#import "UIImageView+WebCache.h"
#import "GuideHomeViewController.h"
#import "CollectDetailsViewController.h"
#import "TAMacro.h"
@interface CollectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)CollectTableViewCell * cell;
@property (nonatomic,strong)NSMutableArray * array;
@end
@implementation CollectViewController
- (NSMutableArray *)array {
    if (!_array) {
        self.array = [NSMutableArray array];
        self.array =  [ToolDB selectFromSql:@"SELECT title,cover_image_url,Id FROM t_activey"];
        if (self.array.count == 0) {
            UIImageView *image  = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            image.backgroundColor = [UIColor redColor];
            image.image = [UIImage imageNamed:@"文字2.jpg"];
            [self.view addSubview:image];
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 4  , SCREEN_HEIGHT / 10, SCREEN_WIDTH , SCREEN_HEIGHT / 10)];
            lable.text = @"您还未收藏任何东西";
            lable.textColor = [UIColor colorWithRed:0.60 green:0.85 blue:0.79 alpha:1.00];
            lable.font = [UIFont systemFontOfSize:20];
            [self.view addSubview:lable];
        }
        [self.tableView reloadData];
    }
    return _array;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"指南收藏";
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    [butt setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    butt.frame  = CGRectMake(0, 0, 50, 50);
    UIBarButtonItem *left= [[UIBarButtonItem alloc]initWithCustomView:butt];
    [butt addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =left;
//    [self nightModel];
    [self foundTableView];
    
}
- (void)foundTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:0];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    self.model = self.array[indexPath.row];
    NSString *sql2 = [NSString stringWithFormat:@"DELETE FROM t_activey where title = '%@' and cover_image_url = '%@';",self.model.title ,self.model.cover_image_url];
    [ToolDB dele:sql2];
    // 查询
    self.array =  [ToolDB selectFromSql:@"SELECT title,cover_image_url,Id FROM t_activey"];
    [tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (self.cell  == nil) {
        self.cell = [[CollectTableViewCell alloc]initWithStyle:0 reuseIdentifier:ID];
    }
    [self.cell jxl_setDayMode:^(UIView *view) {
        self.cell.backgroundColor =  [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        self.cell.backgroundColor =  [UIColor grayColor];
    }];
    self.model = self.array[indexPath.row];
    self.cell.name.text = self.model.title;
    [self.cell.photo sd_setImageWithURL:[NSURL URLWithString:self.model.cover_image_url] placeholderImage:[UIImage imageNamed:@"站位"]];
    self.cell.ID.text = self.model.Id;
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_WIDTH / 2;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.hidesBottomBarWhenPushed = YES;
    CollectDetailsViewController *guideHomeVC =[[CollectDetailsViewController alloc]init];
    self.model = self.array[indexPath.row];
    guideHomeVC.str = self.model.Id;
    [self.navigationController pushViewController:guideHomeVC animated:YES];
    self.hidesBottomBarWhenPushed = YES;
}
- (void)clickLeft:(UIButton*)qwe {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)nightModel {
    //设置日间和夜间两张状态
    [self.view jxl_setDayMode:^(UIView *view) {
        //设置日间模式
        view.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        view.backgroundColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
    }];
    [self.navigationController.navigationBar jxl_setDayMode:^(UIView *view) {
        UINavigationBar *bar = (UINavigationBar *)view;
        //改变状态栏前景颜色
        bar.barStyle = UIBarStyleDefault;
        bar.barTintColor = [UIColor colorWithRed:0.98 green:0.29 blue:0.31 alpha:1.00];
    } nightMode:^(UIView *view) {
        UINavigationBar *bar = (UINavigationBar *)view;
        //改变状态栏前景颜色
        bar.barStyle = UIBarMetricsDefault;
        bar.barTintColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
    }];
    self.view.backgroundColor = [UIColor grayColor];
}



@end
